<?php

namespace App\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{
    //
    public function index()
    {
        $session = Session::get('user');
        $user = Admin::where('id',$session['uid'])->first()->toArray();

        return view('index')->with('user',$user);
    }

    /**
     * 用户注册操作
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function register(Request $request)
    {
        if($request->isMethod('post'))
        {
            $this->validate($request,[
                'name' => 'required|min:3|unique:admin,name',
                'password' => 'required|min:6|confirmed',
                'password_confirmation' => 'required|min:6'
            ]);

            //验证通过注册数据库
            $admin = new Admin();
            $admin->name = $request->input('name');
            $admin->password = md5($request->input('password'));
            $admin->user_pic = '/image/tou.jpg'; //默认头像
            $admin->save();

            //生成session保存信息
            Session::put('user',[
                'uid' => $admin->id,
                'name' => $admin->name
            ]);
        }

        if(Session::get('user'))
        {
            return redirect(url('index')); //跳转到登录页
        }

        return view('register');
    }

    /**
     * 用户登录操作
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        if($request->isMethod('post'))
        {
            $this->validate($request,[
                'name' => 'required|min:3',
                'password' => 'required|min:6'
            ]);
            $admin = Admin::where('name',$request->name)->first()->toArray();
            if(md5($request->password) != $admin['password'])
            {
                return redirect(url('login'))->with('passNot','用户名或密码不正确');
            }
            Session::put('user',[
                'uid' => $admin['id'],
                'name' => $admin['name']
            ]);
            return redirect(url('index'));
        }
        return view('login');
    }

    /**
     * 退出登录
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout()
    {
        Session::pull('user', '');
        return redirect(url('login'));
    }
    /**
     * 修改用户信息
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function upUser(Request $request)
    {
        $up = [];
        $file = $request->file('pic');
        if($file->isValid())
        {
            // 获取文件相关信息
            $ext = $file->getClientOriginalExtension(); // 扩展名
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            $file->move('./image',$filename);
            $up['user_pic'] = '/image/'.$filename;
        }
        $up['name'] = $request->name;
        Admin::where('id',$request->id)->update($up);

        return redirect('index');
    }
}

